package com.example.queryexpressdelivery_01.common;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.example.queryexpressdelivery_01.entity.AdminData;
import com.example.queryexpressdelivery_01.entity.UserData;

import java.util.ArrayList;
import java.util.List;

public class DeliverySQLiteOpenHelper extends SQLiteOpenHelper {
    public DeliverySQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version){
        super(context,name,factory, version);
    }
    public DeliverySQLiteOpenHelper(Context context){
        super(context,"delivery.db",null,1);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        // 创建表user来保存用户的数据，admin来保存管理员的信息
        String createTableSQLUser="create table if not exists user "+
                "(id integer primary key autoincrement,account text not null,"+
                "phone text not null,password text not null,email text, " +
                "data text,lastlogintime text,status text not null)";
        db.execSQL(createTableSQLUser);

        String createTableSQLAdmin="create table if not exists admin "+
                "(id integer primary key autoincrement,account text not null,"+
                "phone text not null,password text not null, email text not null)";
        db.execSQL(createTableSQLAdmin);
        insertInitialData(db);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        //升级数据库，例如添加新的列

    }
    private void insertInitialData(SQLiteDatabase db){
        //String account,String phone,String password,String email,String data,String lastlogintime,String status
        UserData user = new UserData("张小三","16666666666","123456",null,null,null,"0");
        ContentValues valuesuser = new ContentValues();
        valuesuser.put("account", user.getAccount());
        valuesuser.put("phone", user.getPhone());
        valuesuser.put("password", user.getPassword());
        valuesuser.put("email", user.getEmail());
        valuesuser.put("data", user.getData());
        valuesuser.put("lastlogintime", user.getLastlogintime());
        valuesuser.put("status", user.getStatus());
        db.insert("user",null,valuesuser);

        //String account,String phone,String password, String email
        //初始化固定3个管理员
        List<AdminData> admins = new ArrayList<>();
        admins.add(new AdminData("admin","10111111111","123456","123@qq.com"));
        admins.add(new AdminData("admin1","10222222222","123456","234@qq.com"));
        admins.add(new AdminData("admin2","10333333333","123456","356@qq.com"));
        for (AdminData admin:admins){
            ContentValues valuesadmin = new ContentValues();
            valuesadmin.put("account", admin.getAccount());
            valuesadmin.put("phone", admin.getPhone());
            valuesadmin.put("password", admin.getPassword());
            valuesadmin.put("email", admin.getEmail());
            db.insert("admin",null,valuesadmin);
        }
    }
}
